var app = getApp();
Page({
  data: {
    DAY: ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"],
    temDay: "星期一",
    sects: {
      "星期一": { "第一节": "", "第二节": "", "第三节": "", "第四节": "", "第五节": "", "第六节": "", "第七节": "", "第八节": "", "第九节": "", "第十节": "" },
      "星期二": { "第一节": "", "第二节": "", "第三节": "", "第四节": "", "第五节": "", "第六节": "", "第七节": "", "第八节": "", "第九节": "", "第十节": "" },
      "星期三": { "第一节": "", "第二节": "", "第三节": "", "第四节": "", "第五节": "", "第六节": "", "第七节": "", "第八节": "", "第九节": "", "第十节": "" },
      "星期四": { "第一节": "", "第二节": "", "第三节": "", "第四节": "", "第五节": "", "第六节": "", "第七节": "", "第八节": "", "第九节": "", "第十节": "" },
      "星期五": { "第一节": "", "第二节": "", "第三节": "", "第四节": "", "第五节": "", "第六节": "", "第七节": "", "第八节": "", "第九节": "", "第十节": "" },
      "星期六": { "第一节": "", "第二节": "", "第三节": "", "第四节": "", "第五节": "", "第六节": "", "第七节": "", "第八节": "", "第九节": "", "第十节": "" },
      "星期天": { "第一节": "", "第二节": "", "第三节": "", "第四节": "", "第五节": "", "第六节": "", "第七节": "", "第八节": "", "第九节": "", "第十节": "" }
    },
    sect1: "",
    sect2: "",
    sect3: "",
    sect4: "",
    sect5: "",
    sect6: "",
    sect7: "",
    sect8: "",
    sect9: "",
    sect10: "",
    hide: true,
    content: "",
    temDayAndSect: { day: "", sect: "" },
    dayShift: null
  }, dayTap(a) {
    this.setData({
      temDay: this.data.DAY[a.currentTarget.dataset.idx],
      sect1: this.data.sects[this.data.DAY[a.currentTarget.dataset.idx]]["第一节"],
      sect2: this.data.sects[this.data.DAY[a.currentTarget.dataset.idx]]["第二节"],
      sect3: this.data.sects[this.data.DAY[a.currentTarget.dataset.idx]]["第三节"],
      sect4: this.data.sects[this.data.DAY[a.currentTarget.dataset.idx]]["第四节"],
      sect5: this.data.sects[this.data.DAY[a.currentTarget.dataset.idx]]["第五节"],
      sect6: this.data.sects[this.data.DAY[a.currentTarget.dataset.idx]]["第六节"],
      sect7: this.data.sects[this.data.DAY[a.currentTarget.dataset.idx]]["第七节"],
      sect8: this.data.sects[this.data.DAY[a.currentTarget.dataset.idx]]["第八节"],
      sect9: this.data.sects[this.data.DAY[a.currentTarget.dataset.idx]]["第九节"],
      sect10: this.data.sects[this.data.DAY[a.currentTarget.dataset.idx]]["第十节"],
      dayShift: a
    })
  }, tapSect1(a) {
    this.setData({
      hide: false,
      temDayAndSect: { day: this.data.temDay, sect: "第一节" }
    })
  }, tapSect2(a) {
    this.setData({
      hide: false,
      temDayAndSect: { day: this.data.temDay, sect: "第二节" }
    })
  }, tapSect3(a) {
    this.setData({
      hide: false,
      temDayAndSect: { day: this.data.temDay, sect: "第三节" }
    })
  }, tapSect4(a) {
    this.setData({
      hide: false,
      temDayAndSect: { day: this.data.temDay, sect: "第四节" }
    })
  }, tapSect5(a) {
    this.setData({
      hide: false,
      temDayAndSect: { day: this.data.temDay, sect: "第五节" }
    })
  }, tapSect6(a) {
    this.setData({
      hide: false,
      temDayAndSect: { day: this.data.temDay, sect: "第六节" }
    })
  }, tapSect7(a) {
    this.setData({
      hide: false,
      temDayAndSect: { day: this.data.temDay, sect: "第七节" }
    })
  }, tapSect8(a) {
    this.setData({
      hide: false,
      temDayAndSect: { day: this.data.temDay, sect: "第八节" }
    })
  }, tapSect9(a) {
    this.setData({
      hide: false,
      temDayAndSect: { day: this.data.temDay, sect: "第九节" }
    })
  }, tapSect10(a) {
    this.setData({
      hide: false,
      temDayAndSect: { day: this.data.temDay, sect: "第十节" }
    })
  }, inputToastBinder(a) {
    this.setData({ content: a.detail.value})
  }, inputToastCancel() {
    this.setData({ hide: true })
  }, inputToastConfirm() {
    this.data.sects[this.data.temDayAndSect.day][this.data.temDayAndSect.sect] = this.data.content
    if (this.data.temDayAndSect.sect == "第一节") {
      this.setData({ sect1: this.data.content })
    }if (this.data.temDayAndSect.sect == "第二节") {
      this.setData({ sect2: this.data.content })
    }if (this.data.temDayAndSect.sect == "第三节") {
      this.setData({ sect3: this.data.content })
    }if (this.data.temDayAndSect.sect == "第四节") {
      this.setData({ sect4: this.data.content })
    }if (this.data.temDayAndSect.sect == "第五节") {
      this.setData({ sect5: this.data.content })
    }if (this.data.temDayAndSect.sect == "第六节") {
      this.setData({ sect6: this.data.content })
    }if (this.data.temDayAndSect.sect == "第七节") {
      this.setData({ sect7: this.data.content })
    }if (this.data.temDayAndSect.sect == "第八节") {
      this.setData({ sect8: this.data.content })
    }if (this.data.temDayAndSect.sect == "第九节") {
      this.setData({ sect9: this.data.content })
    }if (this.data.temDayAndSect.sect == "第十节") {
      this.setData({ sect10: this.data.content })
    }this.setData({content:'',hide:true})
    wx.cloud.callFunction({ name: 'function', data: { type: "AddOrUpdateCourse", class:app.globalData.userInfo.class, department:app.globalData.userInfo.department, course: this.data.sects}})
  }, onLoad() {
    if (app.globalData.userInfo.class && app.globalData.userInfo.department) {
      wx.cloud.callFunction({name: 'function', data: {type: "get",table:"course",where:{class: app.globalData.userInfo.class,department: app.globalData.userInfo.department}}}).then(res => {
        this.setData({sects: (res.result.user.data[0] != null) ? res.result.user.data[0].course : this.data.sects})
        if (this.data.dayShift == null) {
          this.setData({
            sect1: this.data.sects["星期一"]["第一节"],
            sect2: this.data.sects["星期一"]["第二节"],
            sect3: this.data.sects["星期一"]["第三节"],
            sect4: this.data.sects["星期一"]["第四节"],
            sect5: this.data.sects["星期一"]["第五节"],
            sect6: this.data.sects["星期一"]["第六节"],
            sect7: this.data.sects["星期一"]["第七节"],
            sect8: this.data.sects["星期一"]["第八节"],
            sect9: this.data.sects["星期一"]["第九节"],
            sect10: this.data.sects["星期一"]["第十节"]
          })
        } else {
          this.setData({
            temDay: this.data.DAY[dayShift.currentTarget.dataset.idx],
            sect1: this.data.sects[this.data.DAY[dayShift.currentTarget.dataset.idx]]["第一节"],
            sect2: this.data.sects[this.data.DAY[dayShift.currentTarget.dataset.idx]]["第二节"],
            sect3: this.data.sects[this.data.DAY[dayShift.currentTarget.dataset.idx]]["第三节"],
            sect4: this.data.sects[this.data.DAY[dayShift.currentTarget.dataset.idx]]["第四节"],
            sect5: this.data.sects[this.data.DAY[dayShift.currentTarget.dataset.idx]]["第五节"],
            sect6: this.data.sects[this.data.DAY[dayShift.currentTarget.dataset.idx]]["第六节"],
            sect7: this.data.sects[this.data.DAY[dayShift.currentTarget.dataset.idx]]["第七节"],
            sect8: this.data.sects[this.data.DAY[dayShift.currentTarget.dataset.idx]]["第八节"],
            sect9: this.data.sects[this.data.DAY[dayShift.currentTarget.dataset.idx]]["第九节"],
            sect10: this.data.sects[this.data.DAY[dayShift.currentTarget.dataset.idx]]["第十节"]
          })
        }
      })
    } else {wx.showToast({title: '请登录并完善个人信息', icon: 'none', duration: 2000, success: function () {setTimeout(function (){wx.reLaunch({ url: '/pages/me/me' })}, 2000)}})}
  }
})